//
//  MessageVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/1/3.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "MessageVC.h"
#import "MessageCell.h"
#import "WebViewVC.h"
#import "TicketOrderDetail.h"
@interface MessageVC ()

@property (nonatomic, strong) NSArray *dataSource;
//@property (nonatomic) NSInteger start;

@end

@implementation MessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self registCells:@[@"MessageCell"]];
    [self setAutoRowHeight];
    self.title = MYLocalizedString(@"ACCOUNT_MESSAGE", nil);
    
    [self configEmptyDataWithEmptyImageName:@"暂无提问" describe:MYLocalizedString(@"CONTENT_NO_MESSAGE", nil) refresh:nil block:nil];
    
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
//        self.start = 0;
        [self loadDataSource];
    }];
    
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        @strongify(self);
//        [self loadDataSource];
//    }];
    
    [self.tableView setThemeStyle:ThemeStyleBlack];
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self loadDataSource];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor clearColor];
    lineView.frame = CGRectMake(0, 0, kScreenWidth, 8);
    
    self.tableView.tableHeaderView = lineView;
}

//- (NSMutableArray *)dataSource {
//
//    if (!_dataSource) {
//        _dataSource = [NSMutableArray array];
//    }
//
//    return _dataSource;
//}

- (void)loadDataSource {
    
    @weakify(self);
    
    [NetAPIManager getMessagesListWithPageno:0 pagesize:1000 Block:^(NSArray<Message *> *listArray, NSError *error) {
        
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        [self.tableView.mj_header endRefreshing];
        self.dataHasLoad = YES;
        
//        if (self.start == 0) {
//            [self.tableView.mj_footer resetNoMoreData];
//            [self.dataSource removeAllObjects];
//        }
        
//        if (listArray.count < 20) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        } else {
//
//            [self.tableView.mj_footer endRefreshing];
//        }
        
//        [self.dataSource addObjectsFromArray:listArray];
        
        self.dataSource = listArray;
        
        [self.tableView reloadData];
        
//        self.start ++;
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Message *msg = self.dataSource[indexPath.row];
    
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell" forIndexPath:indexPath];
    cell.msg = msg;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // type 消息类型:answer,ticket,other,gotoweb
    
    Message *msg = self.dataSource[indexPath.row];

    if ([msg.type isEqualToString:@"answer"]) {
        
        UIViewController *vc = [IGUtil loadViewController:@"AccountMyQuestion" storyboard:@"Account"];
        [self.navigationController pushViewController:vc animated:YES];
        
    } else if ([msg.type isEqualToString:@"ticket"]) {
        
        TicketOrderDetail * detailVC = [[TicketOrderDetail alloc] init];
        
        detailVC.hidesBottomBarWhenPushed = YES;
        detailVC.ticketOrderID = msg.relationid;
        
        [self.navigationController pushViewController:detailVC animated:YES];
        
    } else if ([msg.type isEqualToString:@"gotoweb"]) {
        
        WebViewVC *webVC = [WebViewVC new];
        
        webVC.url = msg.relationid;
        webVC.title = msg.title;
        [self.navigationController pushViewController:webVC animated:YES];
    }
    
}


//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    
//    return 8.0f;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    
//    return 0.1f;
//}

@end
